package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameSuit.*;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.suit.Suit;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.ErrorWord;

import java.util.List;

public class SuitHandler {
    // CMD_GAME_PARTNER_SUIT_UNLOCK = 70602; // 服装解锁
    public static Object CMD_GAME_PARTNER_SUIT_UNLOCK(RequestParam clientParam) throws Exception {
        CSGameSuitUnlockRequest request = CSGameSuitUnlockRequest.parseFrom(clientParam.getData());
        CSGameSuitUnlockResponse.Builder response = CSGameSuitUnlockResponse.newBuilder();
        long userId = clientParam.getUserId();

        int suitId = request.getSuitId();
        Suit suit = SuitManager.getSuit(suitId);
        if (suit == null) {
            return ErrorWord.NOT_DATA;
        }
        UserSuit userSuit = SuitManager.getUserSuit(userId, suitId);
        if (userSuit != null) {
            return ErrorWord.REPEATED_OPERATION;// 该服装已经解锁了
        }

        List<RewardItem> unlockItewm = CommonUtils.takeReawrdItemFromStr(suit.getUnlockItem());// 需要消耗的道具
        UserInfo userInfo = GameUser.getUserInfo(userId);
        if (!BagManager.checkNeedItemNum(userInfo, unlockItewm)) {
            return ErrorWord.ITEM_LACK;// 道具不足
        }

        // 执行逻辑
        PBUserData.Builder pbUserData = response.getUserBuilder();
        RewardManager.subReward(userInfo, unlockItewm, pbUserData, LogType.SUIT_UNLOCK);
        RewardManager.addReward(userInfo, suitId, 1, pbUserData, LogType.SUIT_UNLOCK);

        return response;
    }

    // CMD_GAME_PARTNER_SUIT_LEVEL_UP = 70603; // 服装升级
    public static Object CMD_GAME_PARTNER_SUIT_LEVEL_UP(RequestParam clientParam) throws Exception {
        CSGameSuitLevelUpRequest request = CSGameSuitLevelUpRequest.parseFrom(clientParam.getData());
        CSGameSuitLevelUpResponse.Builder response = CSGameSuitLevelUpResponse.newBuilder();
        long userId = clientParam.getUserId();

        int suitId = request.getSuitId();
        Suit suit = SuitManager.getSuit(suitId);
        if (suit == null) {
            return ErrorWord.NOT_DATA;
        }
        UserSuit userSuit = SuitManager.getUserSuit(userId, suitId);
        if (userSuit == null) {
            return ErrorWord.BAD_PARAM;// 未解锁
        }
        int curLevel = userSuit.getLevel();
        if (curLevel + 1 > suit.getMaxLevel()) {
            return ErrorWord.ALREADY_MAX;// 最大等级
        }
        List<RewardItem> upgradeItem = CommonUtils.takeReawrdItemFromStr(suit.getUpgradeItem());// 需要消耗的道具
        UserInfo userInfo = GameUser.getUserInfo(userId);
        if (!BagManager.checkNeedItemNum(userInfo, upgradeItem)) {
            return ErrorWord.ITEM_LACK;// 道具不足
        }

        // 执行逻辑
        PBUserData.Builder pbUserData = response.getUserBuilder();
        RewardManager.subReward(userInfo, upgradeItem, pbUserData, LogType.SUIT_LEVEL_UP);
        userSuit.addLevel(1);// 技能等级+1
        userSuit.update();
        ProtoDataUtils.updatePBUserData(pbUserData, userSuit);

        return response;
    }

}
